﻿using ModbusCase.Lib;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ModbusCase
{
    /// <summary>
    /// modbus RTU 通讯库 (经优化的封装测试)
    /// </summary>
    public partial class TestRTUNew : Window
    {
        public TestRTUNew()
        {
            InitializeComponent();
        }

        private async void Btn01_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTUNew modbusRTU = new ModbusRTUNew(serialPort);
            //modbusRTU.ReadBools(1, FuncCode.ReadCoils, 0, 3);

            // _ 代表不关心结果(丢弃返回值)
            //_ = modbusRTU.ReadBoolsAsync(1, FuncCode.ReadCoils, 0, 3);

            bool[] bools = await modbusRTU.ReadBoolsAsync(1, FuncCode.ReadCoils, 0, 3);
        }

        private void Btn02_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTUNew modbusRTU = new ModbusRTUNew(serialPort);
            modbusRTU.ReadBools(1, FuncCode.ReadDiscreteInputs, 0, 3);
        }

        private void Btn03_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTUNew modbusRTU = new ModbusRTUNew(serialPort);
            modbusRTU.ReadReg16(1, FuncCode.ReadHoldingRegisters, 0, 3);
        }

        private void Btn04_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTUNew modbusRTU = new ModbusRTUNew(serialPort);
            modbusRTU.ReadReg16(1, FuncCode.ReadInputRegisters, 0, 3);
        }

        private void Btn03_32_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTUNew modbusRTU = new ModbusRTUNew(serialPort);
            modbusRTU.ReadRegInt32(1, FuncCode.ReadHoldingRegisters, 0, 3, Endian.BigEndian);
            //modbusRTU.ReadRegInt32(1,FuncCode.ReadHoldingRegisters, 0, 3, Endian.LittleEndian);

            //modbusRTU.ReadReg32Float(1,FuncCode.ReadHoldingRegisters, 0, 3, Endian.BigEndian);
            //modbusRTU.ReadReg32Float(1,FuncCode.ReadHoldingRegisters, 0, 3, Endian.LittleEndian);
        }

        private void Btn04_32_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTUNew modbusRTU = new ModbusRTUNew(serialPort);

            modbusRTU.ReadRegInt32(1, FuncCode.ReadInputRegisters, 0, 3, Endian.BigEndian);
            //modbusRTU.ReadRegInt32(1,FuncCode.ReadInputRegisters, 0, 3, Endian.LittleEndian);

            //modbusRTU.ReadReg32Float(1,FuncCode.ReadInputRegisters, 0, 3, Endian.BigEndian);
            //modbusRTU.ReadReg32Float(1,FuncCode.ReadInputRegisters, 0, 3, Endian.LittleEndian);
        }

        private void btn05_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTUNew modbusRTU = new ModbusRTUNew(serialPort);
            modbusRTU.WriteSingleCoil(1, 0, true);
        }

        private void btn06_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTUNew modbusRTU = new ModbusRTUNew(serialPort);
            modbusRTU.WriteSingleReg(1, 0, 123);
        }

        private void btn0F_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTUNew modbusRTU = new ModbusRTUNew(serialPort);
            modbusRTU.WriteMultipleCoil(1, 0, [true, false, true]);
        }

        private void btn10_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTUNew modbusRTU = new ModbusRTUNew(serialPort);
            modbusRTU.WriteMultipleReg32Int(1, 0, [123, 456, 789], Endian.BigEndian);
        }

        private void btn10_float_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTUNew modbusRTU = new ModbusRTUNew(serialPort);
            modbusRTU.WriteMultipleReg32Float(1, 0, [123.2F, 456.3F, 789.4F], Endian.BigEndian);
        }
    }
}
